// pages/login/login.js
const app = getApp()
const wx_auth = require('../../common/wx_auth') //微信授权js
const util = require('../../utils/util')
const $get_api = require('../../common/api').HTTP;
const setInfo = require('../../const').SET_INFO;
const share_obj = setInfo.share_obj;
// const $api = require('../../common/api.js').HTTP;
const page_name = 'personData/'
Page({

    /**
     * 页面的初始数据
     */
    data: {
        wx_auth:wx_auth.authApply,
        util:util,
        userInfo:{},
        page:'login',
        code:"",
        isLoginBtn:true,
        isPhoneLoginBtn:true,
        right_row:app.imgUrl + "right_row.png"+ app.version,
        add_icon:app.imgUrl + page_name +  "add_icon.png"+ app.version,
        person_list:[
            {id:1,icon:app.imgUrl + "youjiantou.png"+ app.version,name:"我的报名",is_show:1},
            {id:2,icon:app.imgUrl + "youjiantou.png"+ app.version,name:"联系方式",is_show:1},
            {id:3,icon:app.imgUrl + "youjiantou.png"+ app.version,name:"关于小程序",is_show:1},
            {id:4,icon:app.imgUrl + "youjiantou.png"+ app.version,name:"关于我们",is_show:1},
        ],
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        // if(app.useConst.LOGO_CONFIG.WX_ISPHONELOGIN == true){
        //     wx.login({
        //         success: res1=>{
        //             this.code = res1.code
        //         }
        //     })
        // }
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {
        
    },
    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function (options) {
        // console.log(wx_auth.checkToken(1),"checkToken(1)");
        console.log(wx.getStorageSync('token'),"checkToken(1)");
        let that = this
        var UserInfo = util.getUserInfo();
        this.data.userInfo = UserInfo;
        that.data.login_status = wx_auth.checkToken()
        if(!that.data.login_status){
            wx_auth.authApply(options,'');
        }
        if(wx_auth.checkToken(1)){
            this.data.userInfo = UserInfo
            console.log(this.data.userInfo,"this.data.userInfo");
            if(this.data.userInfo.user_type == 1){
              const filteredWords = this.data.person_list.filter((word, index) => {
                
                if(index > 0 ){
                  word.is_show = 0
                }
                return index; // index % 2 === 0  奇偶数条件判断
              });
            }
            this.data.isLoginBtn = false
            this.setData({
              userInfo:this.data.userInfo,
              isLoginBtn:this.data.isLoginBtn,
              person_list: this.data.person_list
            })
            this.navShow();
        }
        
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {
        let _this = this;
        // 构建页面参数 分享公共标题 公共跳转index 公共默认图以及不带参数
        return util.wx_share('','','','',{})
    },
    /**
     * 授权
     */
    getUserProfile: function (options) {
        let that = this;
        // wx_auth.authApply(options,this.data.page,app.useConst.LOGO_CONFIG.WX_LOGIN_TYPE);
        wx_auth.authApply(options,'');
    },
    /*手机号授权*/
    getPhoneNumber:function(options) {
        wx_auth.authApply(options,this.data.page,app.useConst.LOGO_CONFIG.WX_PHONELOGIN_TYPE);
        // this.data.isPhoneLoginBtn = false;
    },
   navShow:function(){
       //控制导航出现
    if (typeof this.getTabBar === 'function' && this.getTabBar()) {
        this.getTabBar().setData({
            selected: 3
        })
    }
   },
   gotoWxavatarurl:function(e){
       wx.navigateTo({
         url: '../wx_avatarUrl/wx_avatarUrl?id=10&cad=13246',
       })
   },
   goto_my_list:function(e){
       let that = this;
       let upage = e.currentTarget.dataset.id;
       console.log(upage,"upage");
       if(upage == 1){
          wx.navigateTo({
            url: '../my_baoming_list/my_baoming_list?id='+upage,
          })
       }
       else if(upage == 2){
        wx.navigateTo({
          url: '../my_Contact/my_Contact?id='+upage,
        })
      }
      else if(upage == 3 || upage == 4 ){
        wx.navigateTo({
          url: '../my_introduce/my_introduce?id='+upage,
        })
      }
   }
})